package top.houry.stack;

import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Deque;

/**
 * @Desc
 * @Author houruiyang
 * @Date 2022/10/31
 **/
public class Test {

    public static void main(String[] args) {
        //要添加水印的图片
        File file = new File("/Users/edy/Desktop/1667370415615.jpeg");
        //添加完水印输出的图片

        String file1 = "/Users/edy/Desktop/img777.png";
        //水印文字
        String st = "你好中国";
        //文字颜色
        Color color = new Color(255,255,255);
        watermark(file, file1, st, color);

//        System.out.println(System.currentTimeMillis());
//
//        System.out.println(getTodayLastTime());
//
//
//        String a = "12312323.4";
//
//
//        Deque



    }
//
//    public static void main(String[] args) {
//        int a  = 1;
//        if (a == 1){
//            System.out.println("123");
//        }else if (a == 2){
//            System.out.println("456");
//        }else {
//            System.out.println("789");
//        }
//    }

//    public static void main(String[] args) {
//        try {
////            File file = new File();
////            //读取图片信息
////            Image t = ImageIO.read(file);
////            //读取图片的宽度
////            int w = t.getWidth(null);
////            //读取图片的高度
////            int h = t.getHeight(null);
//
//            Thumbnails.of("/Users/edy/Desktop/img2.png")
//                    .scale(1D)
//                    .outputQuality(0.5f)
//                    .allowOverwrite(true)
//                    .toFile("/Users/edy/Desktop/com-img3.png");
//
//            byte[] bytes = FileUtils.readFileToByteArray(new File("/Users/edy/Desktop/img2.png"));
//            byte[] bytes1 = compressPicForScale(bytes, 400);
//            FileUtils.writeByteArrayToFile(new File("/Users/edy/Desktop/com-img3.png"),bytes1);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }


    public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
        if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
            return imageBytes;
        }
        long srcSize = imageBytes.length;
        double accuracy = getAccuracy(srcSize / 1024);
        try {
            while (imageBytes.length > desFileSize * 1024) {
                ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
                Thumbnails.of(inputStream)
                        .scale(accuracy)
                        .outputQuality(accuracy)
                        .toOutputStream(outputStream);
                imageBytes = outputStream.toByteArray();
            }
//            LOGGER.info("【图片压缩】 图片原大小={}kb | 压缩后大小={}kb", srcSize / 1024, imageBytes.length / 1024);
        } catch (Exception e) {
//            LOGGER.error("【图片压缩】msg=图片压缩失败!", e);
        }
        return imageBytes;
    }

    private static double getAccuracy(long size) {
        double accuracy;
        if (size < 900) {
            accuracy = 0.85;
        } else if (size < 2047) {
            accuracy = 0.6;
        } else if (size < 3275) {
            accuracy = 0.44;
        } else {
            accuracy = 0.4;
        }
        return accuracy;
    }

    public static Date getTodayLastTime() {
        LocalDateTime lastTimeOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        return localDateTimeToDate(lastTimeOfDay);
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static void watermark(File file, String file1, String st, Color color) {
        try {
            //读取图片信息
            Image t = ImageIO.read(file);
            //读取图片的宽度
            int w = t.getWidth(null);
            //读取图片的高度
            int h = t.getHeight(null);

            //建立画布开始绘画
            BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            //建立画笔
            Graphics2D graphics2D = bufferedImage.createGraphics();
            //将原图片的信息画入画布
            graphics2D.drawImage(t, 0, 0, w, h, null);
            //设置字体属性

            Font font = new Font("华文仿宋", Font.PLAIN, (int) (w * 0.02));

            //设置画笔颜色
            graphics2D.setColor(color);
            //设置画笔字体属性
            graphics2D.setFont(font);
            //设置字体坐标
            int x = w / 30;
            int y = h / 10;

            //将水印画上
            graphics2D.drawString("2022/10/31" + "  " + "10:07:16", (int) (x + w * 0.01), y);
            graphics2D.drawString("E114 38.483, N38 1.931", x, (int) (y + (w * 0.04)));
            graphics2D.drawString("冀AG737学, 17KM/H", x, (int) (h - (h * 0.09)));

            //清空画笔释放资源
            graphics2D.dispose();
            //建立写入文件流
            FileOutputStream out = new FileOutputStream(file1);
            //将画布内容写入到目标文件中，格式为PNG
            ImageIO.write(bufferedImage, "png", out);
            //清空缓冲区
            out.flush();
            //关闭缓冲区
            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
